//
//  TCPTestController.m
//  LazyHome
//
//  Created by yh on 2018/3/30.
//  Copyright © 2018年 yh. All rights reserved.
//

#import "TCPTestController.h"
#import "TCPClient.h"
#import "LHMessage.h"
#import <MJExtension/MJExtension.h>
#import "DeviceTCPManager.h"
#import "TestTool.h"

@interface TCPTestController ()<TCPClientDelegate>
@property (weak, nonatomic) IBOutlet UITextField *hostTF;
@property (weak, nonatomic) IBOutlet UITextField *portTF;


@property (weak, nonatomic) IBOutlet UITextField *sendTV;
@property (weak, nonatomic) IBOutlet UITextView *sendArea;
@property (weak, nonatomic) IBOutlet UITextView *reciveArea;
@property (weak, nonatomic) IBOutlet UIButton *sendBtn;


@property (weak, nonatomic) IBOutlet UIButton *connectBtn;
@property (nonatomic, strong) TCPClient *current_client;
@property (nonatomic, strong) NSMutableArray<TCPClient *> *connectClient;



@end

@implementation TCPTestController

- (void)viewDidLoad {
    [super viewDidLoad];
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(messageCame:) name:NOTI_TCP_MESSAGE_CAME object:nil];
    // Do any additional setup after loading the view.
    self.connectClient = [[NSMutableArray alloc] init];
    
}
- (IBAction)connect:(id)sender {
    self.current_client = nil;
    NSError *error = nil;
    self.current_client =  [[DeviceTCPManager shareManager] creatClientWithHost:self.hostTF.text port:self.portTF.text localport:@"9999" error:&error];
    [self.current_client connectWithError:&error];
}

//- (BOOL)tcpClient:(TCPClient *)tcpClient shouldReconnectForNetChange:(AFNetworkReachabilityStatus)networkReachabilityStatus{
//    return YES;
//}
//
//- (void)tcpClient:(TCPClient *)tcpClient socket:(GCDAsyncSocket *)sock didWriteData:(NSData *)data withTag:(long)tag{
//    NSStringEncoding strEncode = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);//解决中文乱码
//    // self.sendArea.text = [self.sendArea.text stringByAppendingString:[[NSString alloc] initWithData:data encoding:strEncode]];
//}
//
//- (void)tcpClient:(TCPClient *)tcpClient socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
//    NSLog(@"收到数据 %@",data);
//    NSStringEncoding strEncode = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);//解决中文乱码
//    NSString *str = [[NSString alloc] initWithData:data encoding:strEncode];
//    if (str) {
//        NSString *ss = [self.reciveArea.text stringByAppendingString:str];
//        if (ss.length > 1000) {
//            ss =[ss substringFromIndex:ss.length - 1000];
//        }
//        self.reciveArea.text = ss;
//    }
//    
//    
//}


- (void)messageCame:(NSNotification *)noti{
    LHMessage *msg = noti.object;
    NSString *string =[[NSString alloc]initWithData:msg.data encoding:NSUTF8StringEncoding];
    self.reciveArea.text = string;
    NSLog(@"sss - %@",string);
}

- (IBAction)sendMsg:(id)sender {
    NSStringEncoding strEncode = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);//解决中文乱码
    NSDictionary *dict = @{@"cmd":@(0x2001),
                           @"devId":@"LH36541224",
                           @"devName":@"哈哈的继电器",
                           @"devType":@"8",
                           @"devStatus":@([self.sendTV.text intValue])
                           };
    

    
    
    //测试数据
    ushort cmd = 0x2001;
    
    
    
//    LHMessage *ms = [LHMessage messageWithData:[[dict mj_JSONString] dataUsingEncoding:NSUTF8StringEncoding] cmd:cmd];
    LHMessage *ms = [LHMessage messageWithData:[TestTool testDataWithCmd:cmd] cmd:cmd];
    
    [self.current_client sendData:[ms messageData] withTimeout:-1 tag:1001];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
